[XEND] Make SXP be compatible with sequencetypes (Tuples and Lists)
authorAlastair Tse <atse@xensource.com>
Thu, 5 Oct 2006 16:29:19 +0000 (17:29 +0100)
committerAlastair Tse <atse@xensource.com>
Thu, 5 Oct 2006 16:29:19 +0000 (17:29 +0100)
Signed-off-by: Alastair Tse <atse@xensource.com>
tools/python/xen/xend/sxp.py

index 9cc8fc973125334109d4bc4406133470c520952f..af7cb9c0c2a7e16649e3d666d8392dd28770f905 100644 (file)
@@ -378,7 +378,7 @@ def atomp(sxpr):
 def show(sxpr, out=sys.stdout):
     """Print an sxpr in bracketed (lisp-style) syntax.
     """
-    if isinstance(sxpr, types.ListType):
+    if isinstance(sxpr, (types.ListType, types.TupleType)):
         out.write(k_list_open)
         i = 0
         for x in sxpr:
@@ -396,7 +396,7 @@ def show(sxpr, out=sys.stdout):
 def show_xml(sxpr, out=sys.stdout):
     """Print an sxpr in XML syntax.
     """
-    if isinstance(sxpr, types.ListType):
+    if isinstance(sxpr, (types.ListType, types.TupleType)):
         element = name(sxpr)
         out.write('<%s' % element)
         for attr in attributes(sxpr):
@@ -419,7 +419,7 @@ def elementp(sxpr, elt=None):
     sxpr sxpr
     elt  element type
     """
-    return (isinstance(sxpr, types.ListType)
+    return (isinstance(sxpr, (types.ListType, types.TupleType))
             and len(sxpr)
             and (None == elt or sxpr[0] == elt))
 
@@ -435,7 +435,7 @@ def name(sxpr):
     val = None
     if isinstance(sxpr, types.StringType):
         val = sxpr
-    elif isinstance(sxpr, types.ListType) and len(sxpr):
+    elif isinstance(sxpr, (types.ListType, types.TupleType)) and len(sxpr):
         val = sxpr[0]
     return val
 
@@ -447,7 +447,7 @@ def attributes(sxpr):
     returns attribute list
     """
     val = []
-    if isinstance(sxpr, types.ListType) and len(sxpr) > 1:
+    if isinstance(sxpr, (types.ListType, types.TupleType)) and len(sxpr) > 1:
         attr = sxpr[1]
         if elementp(attr, k_attr_open):
             val = attr[1:]
@@ -477,7 +477,7 @@ def children(sxpr, elt=None):
     returns children (filtered by elt if specified)
     """
     val = []
-    if isinstance(sxpr, types.ListType) and len(sxpr) > 1:
+    if isinstance(sxpr, (types.ListType, types.TupleType)) and len(sxpr) > 1:
         i = 1
         x = sxpr[i]
         if elementp(x, k_attr_open):
@@ -566,7 +566,7 @@ def with_id(sxpr, id, val=None):
 
     return s-exp or val
     """
-    if isinstance(sxpr, types.ListType):
+    if isinstance(sxpr, (types.ListType, types.TupleType)):
         for n in sxpr:
             if has_id(n, id):
                 val = n
@@ -586,7 +586,7 @@ def child_with_id(sxpr, id, val=None):
 
     return s-exp or val
     """
-    if isinstance(sxpr, types.ListType):
+    if isinstance(sxpr, (types.ListType, types.TupleType)):
         for n in sxpr:
             if has_id(n, id):
                 val = n
@@ -608,7 +608,7 @@ def elements(sxpr, ctxt=None):
     yield (sxpr, ctxt)
     i = 0
     for n in children(sxpr):
-        if isinstance(n, types.ListType):
+        if isinstance(n, (types.ListType, types.TupleType)):
             # Calling elements() recursively does not generate recursively,
             # it just returns a generator object. So we must iterate over it.
             for v in elements(n, (i, sxpr, ctxt)):